<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>商品品牌管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        #modifyForm{
            padding: 20px 20px 0 0;
            display: none;
        }
        #modifyForm .layui-form-label{
            width: 110px;
        }
        @media screen and (max-width: 450px){
            #modifyForm .layui-form-item .layui-input-inline {
                margin-left: 140px;
            }
        }
        #modifyForm .layui-input-block{
            margin-left: 140px;
        }
        .layui-layer-page .layui-layer-content{
            overflow: unset!important;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品品牌</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input" placeholder="商品品牌" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>
    </div>
    
    <div id="modifyForm">
        <form class="layui-form" action="" lay-filter="modifyForm">
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="redtext marginRight">*</span>商品品牌：</label>
                <div class="layui-input-block">
                  <input type="text" name="brandName" autocomplete="off" placeholder="请输入商品品牌" class="layui-input" maxlength="20">
                </div>
            </div>
        </form>
    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'laydate'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate;
        var excelList,currPage

        //时间
        laydate.render({
            elem: '#selectTime'
            ,theme: '#DCB982'
            ,range: "-"
        });

        // 后台用户列表
        var selectTable;
        selectTable = http.table({
            elem: '#selectTable',
            url: 'admin/product/brand/listPage',
            method: 'get',
            id: 'selectTable',
            toolbar: '#toolbarDemo',
            done: function(res, curr, count){
			    excelList = res.data;
                currPage = curr
			},
            cols: [[
                {title:'序号',type:'numbers',fixed: 'left'}
                ,{field:'name', title: '商品品牌', align : 'center'}
                ,{field:'', title: '操作', align : 'center',fixed: 'right',width: 230,
                    templet: function (d) {
                        var html = '';
                        html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="modify" >修改</button>'
                        html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" >删除</button>'
                        return html;
                    }
                }
            ]]
        });
    

        //查询
        form.on('submit(searchBth)', function (data) {
            let _data = data.field,
				param = _data;
			selectTable.reload({
				page:{curr:1},
				where: param
			})
        })

        // table操作
        table.on('tool(selectTable)', function(obj){
            var data = obj.data;
            if(obj.event=='modify'){
                // 修改
                $("#modifyForm .layui-form")[0].reset();
                form.val('modifyForm',{
                    brandName: data.name
                })
                http.myOpen({
                    title:'修改品牌',
                    content: $('#modifyForm'),
                    area:['500px','300px'],
                    btn:['确定','取消'],
                    btn1:function(index, layero){
                        http.post('admin/product/brand/update',{
                            id: data.id,
                            name: $('#modifyForm input[name=brandName]').val()
                        },function(res){
                            if(res.code == 200){
                                layer.msg('操作成功')
                                layer.close(index)
                                table.reload('selectTable');
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                    }
                })
            }else if(obj.event=='del'){
                // 删除
                http.myOpen({
                    title:'提示',
                    content:'<p style="text-align:center;padding-top:20px">确认要删除该品牌？</p>',
                    area:['300px','150px'],
                    btn:['确定','取消'],
                    btn1:function(index, layero){
                        http.post('admin/product/brand/delete',{
                            id: data.id
                        },function(res){
                            if(res.code == 200){
                                layer.msg('操作成功')
                                layer.close(index)
                                table.reload('selectTable');
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                        
                    }
                });
            }
        })

        // table的toolbar
        table.on('toolbar(selectTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data; //获取选中行数据
            if(obj.event=='add'){ 
                // 新增
                $("#modifyForm .layui-form")[0].reset();
                http.myOpen({
                    title:'新增品牌',
                    content: $('#modifyForm'),
                    area:['500px','300px'],
                    btn:['确定','取消'],
                    btn1:function(index, layero){
                        http.post('admin/product/brand/add',{
                            brandName: $('#modifyForm input[name=brandName]').val()
                        },function(res){
                            if(res.code == 200){
                                layer.msg('操作成功')
                                layer.close(index)
                                table.reload('selectTable');
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                    }
                })
            }
        })

    })

</script>
</html>